// https://nuxt.com/docs/api/configuration/nuxt-config
import { defineNuxtConfig } from "nuxt/config";
import { fileURLToPath, URL } from "url";

export default defineNuxtConfig({
  compatibilityDate: "2025-07-15",
  devtools: { enabled: import.meta.dev },

  // 减少依赖弃用警告
  features: {
    inlineStyles: false,
  },
  modules: [
    "@nuxt/eslint",
    "@nuxtjs/color-mode",
    "@nuxtjs/tailwindcss",
    "@nuxt/icon",
    "@element-plus/nuxt",
  ],

  // CSS 配置
  css: ["~/styles/global.css", "element-plus/theme-chalk/dark/css-vars.css"],

  // 服务端渲染配置
  ssr: true,

  // 应用配置
  app: {
    head: {
      title: "个人博客",
      meta: [
        { charset: "utf-8" },
        { name: "viewport", content: "width=device-width, initial-scale=1" },
        { name: "description", content: "分享技术见解和项目经验的个人博客" },
      ],
      link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
    },
  },

  alias: {
    server: fileURLToPath(new URL("./server", import.meta.url)),
    styles: fileURLToPath(new URL("./app/styles", import.meta.url)),
  },

  /**
   * 颜色模式配置
   *
   * 配置网站的暗色/亮色主题切换功能
   */
  colorMode: {
    preference: "system", // 默认跟随系统主题
    fallback: "light", // 如果无法检测系统主题，则使用亮色主题
    classSuffix: "", // 移除默认的类名后缀
    classPrefix: "", // 使用默认的类名前缀 (dark:)
    storageKey: "userTheme", // 存储用户主题偏好的localStorage键名，与我们的cookie名称保持一致
  },

  // 路由配置
  routeRules: {
    "/": {
      redirect: "/home",
    },
    // 博客页面配置 (移除预渲染，因为动态路由无法预渲染)
    // "/project/:id": { prerender: true },
    // API路由
    "/api/**": { cors: true },
  },

  // TypeScript 配置
  typescript: {
    strict: true,
    typeCheck: false,
  },

  // Nitro 配置
  nitro: {
    experimental: {
      wasm: true,
    },
    // 明确指定存储选项
    storage: {
      redis: {
        driver: "redis",
      },
    },
    // 文件上传配置
    routeRules: {
      "/uploads/**": {
        headers: {
          "Cache-Control": "public, max-age=31536000",
        },
      },
    },
  },

  // 运行时配置
  runtimeConfig: {
    // 数据库配置
    db: {
      DB_HOST: process.env.DB_HOST,
      DB_PORT: process.env.DB_PORT,
      DB_USER: process.env.DB_USER,
      DB_PASSWORD: process.env.DB_PASSWORD,
      DB_NAME: process.env.DB_NAME,
    },
    // JWT 密钥
    JWT_SECRET: process.env.JWT_SECRET,
    rootPath: process.cwd(),
    // 文件上传配置
    fileUpload: {
      STORAGE_TYPE: process.env.STORAGE_TYPE,
      UPLOAD_PATH: process.env.UPLOAD_PATH,
      MAX_FILE_SIZE: process.env.MAX_FILE_SIZE,
    },
    // minio
    minio: {
      MINIO_ENDPOINT: process.env.MINIO_ENDPOINT,
      MINIO_PORT: process.env.MINIO_PORT,
      MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY,
      MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY,
      MINIO_BUCKET_NAME: process.env.MINIO_BUCKET_NAME,
      MINIO_USE_SSL: process.env.MINIO_USE_SSL,
    },
    // 七牛云
    qiniu: {
      QINIU_ACCESS_KEY: process.env.QINIU_ACCESS_KEY,
      QINIU_SECRET_KEY: process.env.QINIU_SECRET_KEY,
      QINIU_BUCKET_NAME: process.env.QINIU_BUCKET_NAME,
      QINIU_DOMAIN: process.env.QINIU_DOMAIN,
    },

    // 公共配置（客户端也可用）
    public: {
      apiBase: "/api",
    },
  },

  devServer: {
    port: 3000,
    host: "0.0.0.0",
  },
});
